package com.naxiang.zhaohang.naxiangvidoplay;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.widget.RelativeLayout;

import com.baidu.cloud.bdrtmpsession.BDRtmpSessionBasic;
import com.baidu.cloud.bdrtmpsession.OnSessionEventListener;
import com.naxiang.zhaohang.config.LiveConfig;
import com.naxiang.zhaohang.session.LiveStreamSession;


/**
 * 作者: 赵航
 * 创建时间: 2018/7/9 15:00
 * 描述:
 */
public class PreviewView extends RelativeLayout implements OnSessionEventListener {
    private SurfaceView mSurfaceView;
    private LiveStreamSession mSession;
    private BDRtmpSessionBasic.UserRole mRole = BDRtmpSessionBasic.UserRole.Host;


    public PreviewView(Context context) {
        super(context,null);
        initView();
    }
    public PreviewView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    private void initView() {


//        binding = DataBindingUtil.setContentView(this, R.layout.activity_streaming);
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.media_controller, this);
        mSurfaceView= (SurfaceView)layout.findViewById(R.id.preview);
//        binding = DataBindingUtil.setContentView(this, R.layout.activity_streaming);
           LiveConfig liveConfig = new LiveConfig.Builder()
//                .setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT) // 选择摄像头为前置摄像头
//                .setCameraOrientation(90) // 设置摄像头为竖向
//                .setVideoWidth(360) // 设置推流视频宽度, 需传入长的一边
//                .setVideoHeight(640) // 设置推流视频高度，需传入短的一边
//                .setVideoFPS(15) // 设置视频帧率
//                .setInitVideoBitrate(400000) // 设置视频码率，单位为bit per seconds
//                .setAudioBitrate(64 * 1000) // 设置音频码率，单位为bit per seconds
//                .setAudioSampleRate(LiveConfig.AUDIO_SAMPLE_RATE_44100) // 设置音频采样率
//                .setGopLengthInSeconds(2) // 设置I帧间隔，单位为秒
//                .setQosEnabled(true) // 开启码率自适应，默认为true，即默认开启
//                .setMinVideoBitrate(200 * 1000) // 码率自适应，最低码率
//                .setMaxVideoBitrate(1024 * 1000) // 码率自适应，最高码率
//                .setQosSensitivity(5) // 码率自适应，调整的灵敏度，单位为秒，可接受[5, 10]之间的整数值
//                .build();
                .setVideoWidth(720)
                .setVideoHeight(1280)
                .setCameraOrientation(90)
                .setVideoFPS(15)
                .setInitVideoBitrate(400000)
                .setMinVideoBitrate(100000)
                .setMaxVideoBitrate(800000)
                .setVideoEnabled(true)
                .setAudioSampleRate(44100)
                .setAudioBitrate(64000)
                .setCameraOrientation(90)
                .setAudioEnabled(true)
                .setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT).build()
                   ;



        mRole = getUserRoleByType(1);
        mSession = new LiveStreamSession(getContext(), liveConfig);
        mSession.setRtmpEventListener(this);
        mSession.setupDevice();
        mSession.setSurfaceHolder(mSurfaceView.getHolder());
        // 1-->主播  2-->高级观众 3-->普通观众
        mSession.configRtmpSession("www.baidu.com" , mRole);
        mSession.startStreaming();

    }

    public SurfaceView getSurfaceView() {
        return mSurfaceView;
    }
    private BDRtmpSessionBasic.UserRole getUserRoleByType(int type) {
        switch (type) {
            case 1:
                return BDRtmpSessionBasic.UserRole.Host;
            case 2:
                return BDRtmpSessionBasic.UserRole.Guest;
            default:
                return BDRtmpSessionBasic.UserRole.Audience;
        }
    }

    @Override
    public void onSessionConnected() {

    }

    @Override
    public void onError(int i) {

    }

    @Override
    public void onConversationRequest(String s, String s1) {


    }

    @Override
    public void onConversationStarted(String s) {

    }

    @Override
    public void onConversationFailed(String s, FailureReason failureReason) {

    }

    @Override
    public void onConversationEnded(String s) {

    }
}
